#!/bin/bash
set -e

Skip=false
EtcDir=/etc
RcsUrl=http://sysrcs.googlecode.com/git
Recipe=recipe
CookCfg=cook.cfg
PkgFileExt=pkg.txz
GlobalSQLite=global.sqlite

. $EtcDir/$CookCfg

PrepareSrc() {
	for i in $RcsDir $SrcDir/$name $PkgDir/$name; do
		if [ ! -d $i ]; then mkdir -p $i; fi
	done

	RcsFile=$RcsDir/$name/$Recipe
	if [ ! -f $RcsFile ]; then
		cd $RcsDir
		echo "info: fetching recipe: $name"
		wget -nv -r -l1 -np -nH --cut-dirs=1 --reject=html $RcsUrl/$name
		echo "      done"
	fi

	. $RcsFile
	srcfile=$(basename $u)
	if [ -z $p ]; then p=$n-$v; fi

	echo "info: preparing source: $n-$v"
	if [ $Skip = false ]; then
		if [ ! -f $TmpDir/$srcfile ]; then
			cd $TmpDir
			echo "      downloading: $srcfile"
			wget -nv $u
		fi

		echo "      extracting: $srcfile"
		tar -C $SrcDir/$name -xf $TmpDir/$srcfile
	fi
	echo "      done"
	cd $BldDir
}

CompileSrc() {
	cd $src/$p
	if type Src >/dev/null 2>&1; then Src; fi
	cd $BldDir
}

CompilePkg() {
	cd $src/$p
	if type Pkg >/dev/null 2>&1; then export -f Pkg; fakeroot Pkg; fi
	cd $BldDir
}

CompilePkgs() {
	cd $src/$p
	if type Pkg_$i >/dev/null 2>&1; then export -f Pkg_$i; fakeroot Pkg_$i; fi
	cd $BldDir
}

InsertDBase() {
	if [ ! -f $SysDir/$GlobalSQLite ]; then
		sqlite3 $SysDir/$GlobalSQLite "create table packages (name text primary key, version text, pkgrp text);"
		sqlite3 $SysDir/$GlobalSQLite "create table filelist (id integer, name text, path text);"
	fi

	sqlite3 $SysDir/$GlobalSQLite "delete from packages where name = '$n';"
	sqlite3 $SysDir/$GlobalSQLite "insert into packages (name, version, pkgrp) values('$n', '$v', '$g');"

	sqlite3 $SysDir/$GlobalSQLite "delete from filelist where name = '$n';"
	sqlite3 -batch $SysDir/$GlobalSQLite <<EOF
.separator ' '
.import $FileList filelist
EOF
}

CompressPkg() {
	echo "info: creating package: $n-$v"
	rm -f $pkg/$shr/info/dir
	cd $pkg

	echo "      stripping files"
	find . -type f | while read _file; do
		case $(file --mime-type -b $_file) in
			*application/x-executable*)
				strip $_file;;
			*application/x-sharedlib*)
				strip $_file;;
			*application/x-archive*)
				strip $_file;;
		esac
	done

	echo "      creating filelist"
	FileList="$PkgDir/filelist.txt"
	if [ -f $FileList ]; then rm -rf $FileList; fi
	lst=$(find . -type f | sed 's/.\//'$n' \//' | sort| cat -n)
	for i in "$lst"; do echo "$i" >> $FileList; done
	sed -e 's/^\s\+//' -e 's/\t/ /' -i $FileList

	echo "      compressing file"
	if [ ! -d $SysDir/$g ]; then mkdir -p $SysDir/$g; fi

	rm -f $SysDir/$g/$n-[0-9]*
	pkgfile=$n-$v-$PkgFileExt
	fakeroot tar -cJf $SysDir/$g/$pkgfile ./

	InsertDBase

	rm -rf $FileList
	rm -rf $PkgDir/$n
	rm -rf $SrcDir/$n

	echo "      done"	
	cd $BldDir
}

MakePackage() {
	pkg=$PkgDir/$name
	rcs=$RcsDir/$name
	src=$SrcDir/$name

	PrepareSrc

	export n v g u p
	export bin etc lib run shr usr var pkg src rcs
	export CHOST CFLAGS CXXFLAGS LDFLAGS MAKEFLAGS

	echo "info: compiling source: $n-$v"
	if [ $Skip = true ]; then
		if [ ${#n[@]} -ge 2 ]; then
			for i in ${n[@]}; do pkg=$PkgDir/$i;
				CompilePkgs
			done
		else
			CompilePkg
		fi
	else
		CompileSrc
		if [ ${#n[@]} -ge 2 ]; then		
			for i in ${n[@]}; do pkg=$PkgDir/$i;
				if [ ! -d $pkg ]; then mkdir -p $pkg; fi
				CompilePkgs
			done
		else
			CompilePkg
		fi
	fi

	if [ ${#n[@]} -ge 2 ]; then
		for i in ${!n[@]}; do n=${n[$i]}; g=${g[$i]}; pkg=$PkgDir/$n;
			CompressPkg
		done
	else
		CompressPkg
	fi
}

if [ -z $1 ]; then echo "warn: missing flag"; exit 0; fi

for i in $@; do
	if [ $i = "--skip-src" ]; then
		Skip=true
	else
		name=$i
		MakePackage
	fi
done